/*
 ============================================================================
 Name        : BlockBengBeng.cpp
 Author      : 
 Version     :
 Copyright   : 
 Description : CBlockTipLogin implementation
 ============================================================================
 */
#include <eStock_L2hy.rsg>

#include "Global.h"
#include "BaseCommon.h"
#include "Block.h"

#include "eStockappui.h"
#include "eStockMainGridContainer.h"

#include "BlockRegister.h"
#include "BlockLogin.h"
#include "BlockTipLogin.h"

CBlockTipLogin::CBlockTipLogin() : CBlock(NULL)
{
	iFouceButton = 0;
	iLeftButtonPress = (0 == iFouceButton);
	iCenterButtonPress = (1 == iFouceButton);
	iRightButtonPress = (2 == iFouceButton);
}

CBlockTipLogin::~CBlockTipLogin()
{
	if (m_vMainInfoTitle != NULL)
	{
		m_vMainInfoTitle->Reset();
		delete m_vMainInfoTitle;
	}

	if (m_vMainInfoText != NULL)
	{
		m_vMainInfoText->Reset();
		delete m_vMainInfoText;
	}

	if (iCheck)
		delete iCheck, iCheck = NULL;
	if (iCheck_MM)
		delete iCheck_MM, iCheck_MM = NULL;
	if (iOffScreenBitmapDevice)
		delete iOffScreenBitmapDevice, iOffScreenBitmapDevice = NULL;
	if (iOffScreenBitmapGc)
		delete iOffScreenBitmapGc, iOffScreenBitmapGc = NULL;
}

CBlockTipLogin* CBlockTipLogin::NewLC()
{
	CBlockTipLogin* self = new (ELeave) CBlockTipLogin();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CBlockTipLogin* CBlockTipLogin::NewL()
{
	CBlockTipLogin* self = CBlockTipLogin::NewLC();
	CleanupStack::Pop(); // self;
	return self;
}

void CBlockTipLogin::ConstructL()
{
	CBlock::ConstructL();
	m_vMainInfoTitle = new (ELeave) CDesC16ArrayFlat(2);
	m_vMainInfoText = new (ELeave) CDesC16ArrayFlat(4);
	CGlobal::GetDesFromID(m_strOK, LOC_BUF_STR_DETAIL);
	CGlobal::GetDesFromID(m_strCenter, LOC_BUF_STR_DETAIL);
	CGlobal::GetDesFromID(m_strCancel, LOC_BUF_STR_CLOSE);
	SizeChanged();
	
	iLeftButtonPress = (0 == iFouceButton);
	iCenterButtonPress = (1 == iFouceButton);
	iRightButtonPress = (2 == iFouceButton);
	//m_threadSocket = BASECOMMON->m_threadSocketInfo;
}

void CBlockTipLogin::SizeChanged()
{
	m_rect.SetRect(15, KTitlebarHeight + 15, m_nScreenWidth - 15,
			m_nScreenHeight - KCbabarHeight - KScrollInfobarHeight - 15);
	m_rect.Shrink(1, 1);
	
	CGlobal::GetDesFromID(m_strMainInfoTitle, LOC_BUF_STR_TIP_LOGIN_TITLE);
	CGlobal::GetDesFromID(m_strMainInfoText, LOC_BUF_STR_TIP_LOGIN_CONTENT);
	
	CGlobal::CreateTipArray(m_strMainInfoTitle, *m_vMainInfoTitle, m_rect.Width() - 20, 0);
	CGlobal::CreateTipArray(m_strMainInfoText, *m_vMainInfoText, m_rect.Width() - 20, 0);

	if (iCheck != NULL)
	{
		delete iCheck, iCheck = NULL;
		delete iCheck_MM, iCheck_MM = NULL;
		delete iOffScreenBitmapDevice, iOffScreenBitmapDevice = NULL;
		delete iOffScreenBitmapGc, iOffScreenBitmapGc = NULL;
	}
	iCheck = new (ELeave) CFbsBitmap();
	iCheck->Create(m_rect.Size(),
			CEikonEnv::Static()->ScreenDevice()->DisplayMode());
	iOffScreenBitmapDevice = CFbsBitmapDevice::NewL(iCheck);
	iOffScreenBitmapDevice->CreateContext(iOffScreenBitmapGc);

	iCheck_MM = new (ELeave) CFbsBitmap;
	iCheck_MM->Create(iCheck->SizeInPixels(), EGray256);
	Transparent();
}

void CBlockTipLogin::Transparent()
{
	CFbsBitmapDevice* bd = CFbsBitmapDevice::NewL(iCheck_MM);
	CleanupStack::PushL(bd);
	CFbsBitGc* gc = NULL;
	User::LeaveIfError(bd->CreateContext(gc));
	gc->SetPenStyle(CGraphicsContext::ENullPen);
	gc->SetBrushStyle(CGraphicsContext::ESolidBrush);
	gc->SetBrushColor(TRgb(230, 230, 230));
	gc->DrawRect(TRect(TPoint(0, 0), iCheck->SizeInPixels()));
	gc->SetBrushStyle(CGraphicsContext::ENullBrush);
	delete gc;
	CleanupStack::PopAndDestroy(bd);
}

void CBlockTipLogin::OnDraw(CWindowGc& gc) const
{
	if (m_vMainInfoTitle->Count() > 0 && m_vMainInfoText->Count() > 0)
	{
		TInt nTitle = m_vMainInfoTitle->Count();
		TInt nText = m_vMainInfoText->Count();
		TInt nSize = nTitle + nText;

		TInt nBottom = m_rect.iBr.iY - 1;
		TInt nTop = m_rect.iTl.iY;

		TBuf<64> str;

		iOffScreenBitmapGc->SetPenStyle(CGraphicsContext::ENullPen);
		iOffScreenBitmapGc->SetBrushStyle(CGraphicsContext::ESolidBrush);
		iOffScreenBitmapGc->SetBrushColor(KRgbBackground);
		iOffScreenBitmapGc->DrawRect(TRect(TPoint(0, 0), m_rect.Size()));

		iOffScreenBitmapGc->SetPenStyle(CGraphicsContext::ESolidPen);
		iOffScreenBitmapGc->SetPenSize(TSize(2, 2));
		iOffScreenBitmapGc->SetPenColor(TRgb(112, 139, 166));
		iOffScreenBitmapGc->SetBrushColor(TRgb(0x22, 0x22, 0x22));
		
		TRect rect(m_rect);
		rect.Shrink(1, 1);
		iOffScreenBitmapGc->DrawRoundRect(TRect(TPoint(0, 0), rect.Size()), TSize(4, 4));
		iOffScreenBitmapGc->SetPenSize(TSize(1, 1));
		gc.BitBltMasked(m_rect.iTl, iCheck, TRect(TPoint(0, 0), iCheck->SizeInPixels()), iCheck_MM, EFalse);

		gc.UseFont(m_pGlobal->g_font);
		for (TInt i = 0; i < nSize; i++)
		{
			TInt nY = nTop + m_nLineHeight * (i + 1);
			TRgb rgb;
			if (i < nTitle)
			{
				gc.SetPenColor(m_pGlobal->g_rgbNameCode);
				str.Copy((*m_vMainInfoTitle)[i]);
			}
			else
			{
				gc.SetPenColor(m_pGlobal->g_rgbText);
				str.Copy((*m_vMainInfoText)[i - nTitle]);
			}
			gc.DrawText(str, TPoint(m_rect.iTl.iX + 10, nY));

			if (nY + m_nLineHeight * 2 + 10 >= nBottom)
				break;
		}

		// draw Buttons
		TInt buttonWidth = (m_rect.Width() - 15 * 4) / 3;
		iLeftButtonRect.SetRect(TPoint(m_rect.iTl.iX + 15, m_rect.iBr.iY - m_nLineHeight - 5),
				TSize(buttonWidth, m_nLineHeight));
		iCenterButtonRect.SetRect(TPoint(m_rect.iTl.iX + 15 * 2 + buttonWidth, m_rect.iBr.iY - m_nLineHeight - 5),
				TSize(buttonWidth, m_nLineHeight));
		iRightButtonRect.SetRect(TPoint(m_rect.iTl.iX + 15 * 3 + buttonWidth * 2,
				m_rect.iBr.iY - m_nLineHeight - 5), TSize(buttonWidth, m_nLineHeight));
		
		if (iLeftButtonPress)
			CBaseBlock::DrawImageRect(gc, iLeftButtonRect, TSize(4, 4), 5);
		else
			CBaseBlock::DrawImageRect(gc, iLeftButtonRect, TSize(4, 4), 2);
		
		if (iCenterButtonPress)
			CBaseBlock::DrawImageRect(gc, iCenterButtonRect, TSize(4, 4), 5);
		else
			CBaseBlock::DrawImageRect(gc, iCenterButtonRect, TSize(4, 4), 2);
		
		if (iRightButtonPress)
			CBaseBlock::DrawImageRect(gc, iRightButtonRect, TSize(4, 4), 5);
		else
			CBaseBlock::DrawImageRect(gc, iRightButtonRect, TSize(4, 4), 2);

		CFont* font;
		CGraphicsDevice* screenDevice = CEikonEnv::Static()->ScreenDevice();
		TFontSpec myFontSpec =
				CEikonEnv::Static()->NormalFont()->FontSpecInTwips();
		myFontSpec.iHeight = screenDevice->VerticalPixelsToTwips(m_nLineHeight - 4);
		screenDevice->GetNearestFontInTwips(font, myFontSpec);
		
		gc.UseFont(font);
		TInt baseLine = iLeftButtonRect.Height() / 2 + font->AscentInPixels()
				- font->HeightInPixels() / 2;
		gc.SetPenColor(KRgbWhite);
		gc.SetBrushStyle(CGraphicsContext::ENullBrush);
		
		CGlobal::GetDesFromID(str, LOC_BUF_STR_REGNOW);
		gc.DrawText(str, iLeftButtonRect, baseLine, CGraphicsContext::ECenter);
		
		CGlobal::GetDesFromID(str, LOC_BUF_MENU_LOGIN2);
		gc.DrawText(str, iCenterButtonRect, baseLine, CGraphicsContext::ECenter);
		
		CGlobal::GetDesFromID(str, LOC_BUF_MENU_CANCEL2);
		gc.DrawText(str, iRightButtonRect, baseLine, CGraphicsContext::ECenter);
		
		gc.DiscardFont();
		screenDevice->ReleaseFont(font);
	}
}

void CBlockTipLogin::OnMouseDown(const TPoint& aPt)
{
	if (iLeftButtonRect.Contains(aPt))
		iLeftButtonPress = ETrue;
	if(iCenterButtonRect.Contains(aPt))
		iCenterButtonPress = ETrue;
	if (iRightButtonRect.Contains(aPt))
		iRightButtonPress = ETrue;
	DrawNow();
}

void CBlockTipLogin::OnMouseUp(const TPoint& aPt)
{
	iLeftButtonPress = EFalse;
	iCenterButtonPress = EFalse;
	iRightButtonPress = EFalse;
	if (iLeftButtonRect.Contains(aPt))
	{
		CBlock* regBlock = CBlockRegister::NewL(NULL);
		GetContainer()->AddBlock(regBlock);
		GetContainer()->DelTipLogin();
		DrawNow();
	}
	else if (iCenterButtonRect.Contains(aPt))
	{
		GetContainer()->AddBlock(CBlockLogin::NewL(NULL,0));
		GetContainer()->DelTipLogin();
		DrawNow();
	}
	else if (iRightButtonRect.Contains(aPt))
	{
		GetContainer()->DelTipLogin();
		DrawNow();
	}
}

void CBlockTipLogin::OnKeyEvent(TInt keyCode)
{
	CGlobal* global = eStockUI->m_Globe;
	if(keyCode == global->g_nKeyLeft)
	{
		iFouceButton --;
		if(iFouceButton <= 0)
			iFouceButton = 0;
		
		iLeftButtonPress = (0 == iFouceButton);
		iCenterButtonPress = (1 == iFouceButton);
		iRightButtonPress = (2 == iFouceButton);
		DrawNow();
	}
	else if(keyCode == global->g_nKeyRight)
	{
		iFouceButton ++;
		if(iFouceButton >= 2)
			iFouceButton = 2;
		
		iLeftButtonPress = (0 == iFouceButton);
		iCenterButtonPress = (1 == iFouceButton);
		iRightButtonPress = (2 == iFouceButton);
		DrawNow();
	}
	else if (keyCode == global->g_nKeyCancel)
	{
		GetContainer()->DelTipLogin();
		DrawNow();
	}
	else if (keyCode == global->g_nKeyOK || keyCode == global->g_nKeySelect)
	{
		if(iLeftButtonPress)
		{
			CBlock* regBlock = CBlockRegister::NewL(NULL);
			GetContainer()->AddBlock(regBlock);
			GetContainer()->DelTipLogin();
			DrawNow();
		}
		else if(iCenterButtonPress)
		{
			GetContainer()->AddBlock(CBlockLogin::NewL(NULL,0));
			GetContainer()->DelTipLogin();
			DrawNow();
		}
		else if(iRightButtonPress)
		{
			GetContainer()->DelTipLogin();
			DrawNow();
		}
	}
}
